package cn.harvest.storage.bo.admin;

import java.util.List;

import cn.harvest.storage.dao.DAO;
import cn.harvest.storage.po.Storehouse;

public class WarehouseService {

	private DAO dao = DAO.getDAO();

	private WarehouseService() {

	}

	public static WarehouseService getService() {

		WarehouseService service = new WarehouseService();
		return service;
	}

	public boolean create(Storehouse storehouse) {

		return dao.create(storehouse);
	}
	
	public boolean update(Storehouse storehouse) {
		
		return dao.update(storehouse);
	}

	public List selectBySQL(Storehouse storehouse) {

		StringBuffer sql = new StringBuffer();
		
		sql.append(" SELECT sh.storehouse_id, sh.storehouse_name, em.employee_name, sh.storehouse_tel, sh.storehouse_add, sh.storehouse_memo, em.employee_id");
		sql.append(" FROM storehouse sh, employee em");
		sql.append(" WHERE em.employee_id = sh.employee_id");
		if (!(null == storehouse.getStorehouseId() || "".equals(storehouse.getStorehouseId()))) {
			sql.append(" AND sh.storehouse_id = '" + storehouse.getStorehouseId() + "' ");
		}
		if (!(null == storehouse.getStorehouseName() || "".equals(storehouse.getStorehouseName()))) {
			sql.append(" AND sh.storehouse_name LIKE '%" + storehouse.getStorehouseName() + "%' ");
		}
		if (!(null == storehouse.getEmployee() || "".equals(storehouse.getEmployee().getEmployeeId()))) {
			sql.append(" AND sh.employee_id = '" + storehouse.getEmployee().getEmployeeId() + "' ");
		}
		sql.append(" ORDER BY sh.storehouse_id");

		System.out.println("### - WarehouseService -> selectBySQL -> #SQL \r\n	 " + sql);
		
		return dao.selectBySQL(sql.toString());
	}
	
	public Storehouse selectByHQL(String shId) {
		StringBuffer hql = new StringBuffer();
		hql.append(" FROM Storehouse sh WHERE sh.storehouseId = " + shId);
		
		System.out.println("### - WarehouseService -> selectByHQL -> #HQL \r\n	 " + hql);
		
		Storehouse sh = (Storehouse) dao.selectByHQL(hql.toString()).get(0);
		return sh;
	}
	
	public List selectWarehouseKeeper() {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT em.employee_id,em.employee_name FROM employee em WHERE em.employment_id = -1");
		
		System.out.println("### - WarehouseService -> selectWarehouseKeeper -> #SQL \r\n	 " + sql);
		
		List keeperList = dao.selectBySQL(sql.toString());
		return keeperList;
	}
	
	
	public static void main(String[] args) {

	}
}
